package com.yanqu.road.server.timer.monopolymarketTask;

import com.yanqu.road.entity.monopolymarket.MonopolyMarket;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrab;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrabFinishData;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrabPointInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketResGrabMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Description 榷场结算资源争夺战
 * @Author cwq
 * @Data 2021/9/3 11:27
 */
public class MonopolyMarketTask  extends GeneralTask {

    @Override
    public void doRun() {
        try {
            synchronized (MonopolyMarketMgr.groupMonopolyMarketMap){
                Set<Long> userSet = new HashSet<>();
                int now = DateHelper.getCurrentSecond();
                for (MonopolyMarket item : MonopolyMarketMgr.groupMonopolyMarketMap.values()){
                    synchronized (item){
                        for (MonopolyMarketResGrab resGrab : item.monopolyMarketResGrab.values()){
                            if(resGrab.minResGrabEndTime != 0 && resGrab.minResGrabEndTime <= now) {
                                resGrab.collectingPointInfoList.sort((t1,t2)->{
                                    return Integer.compare(t1.baseInfo.getEndTime(),t2.baseInfo.getEndTime());
                                });
                                while (resGrab.collectingPointInfoList.size() > 0){
                                    MonopolyMarketResGrabPointInfo pointInfo = resGrab.collectingPointInfoList.get(0);
                                    long userId = pointInfo.baseInfo.getUserId();
                                    boolean end = MonopolyMarketResGrabMgr.attemptSettlement(item,resGrab,pointInfo,now);
                                    if(end){
                                        userSet.add(userId);
                                        resGrab.minResGrabEndTime = 0;
                                    }else {
                                        MonopolyMarketResGrabMgr.setMinResGrabEndTime(resGrab,pointInfo);
                                        break;
                                    }
                                }
                            }
                        }
                        // 尝试刷新榷场
                        MonopolyMarketMgr.refreshMonopolyMarket(item,now);
                    }
                }
                // 通知这些玩家有奖励结算资源点
                for (long userId : userSet){
                    UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
                    MessageHelper.sendPacket(userBaseInfo.getServerId(), userId , YanQuMessageUtils.buildMessage(GameProtocol.S_MONOPOLY_MARKET_NORMAL_SETTLEMENT_NOTIFY_FROM_CROSS));
                }
            }
            // 给各个区服发送需要加入排行榜的值
            HashMap<Long, MonopolyMarketProto.MonopolyMarketAddRankValueToGameServerMsg.Builder> map = new HashMap<>();
            synchronized (MonopolyMarketResGrabMgr.userResGrabRankMap){
                for (MonopolyMarketResGrabFinishData item : MonopolyMarketResGrabMgr.userResGrabRankMap.values()){
                    if(item.getIsJoinRank()){
                       continue;
                    }
                    long serverId = CrossUserMgr.getServerIdByUserId(item.getUserId());
                    MonopolyMarketProto.MonopolyMarketAddRankValueToGameServerMsg.Builder toGameMsg = map.get(serverId);
                    if(toGameMsg == null){
                        toGameMsg = MonopolyMarketProto.MonopolyMarketAddRankValueToGameServerMsg.newBuilder();
                        map.put(serverId, toGameMsg);
                    }
                    MonopolyMarketProto.MonopolyMarketAddRankValueEntity.Builder addRankValueEntity = MonopolyMarketProto.MonopolyMarketAddRankValueEntity.newBuilder();
                    addRankValueEntity.setId(item.getId());
                    addRankValueEntity.setUserId(item.getUserId());
                    addRankValueEntity.setAddValue(item.getRewardNum());
                    addRankValueEntity.setType(item.getType());
                    // 该记录采集时间
                    addRankValueEntity.setTime(item.getTotalTime());
                    // 该记录采集的开始时间
                    addRankValueEntity.setStartTime(item.getStartTime());
                    toGameMsg.addAddRankList(addRankValueEntity);
                }
            }
            for (Map.Entry<Long,MonopolyMarketProto.MonopolyMarketAddRankValueToGameServerMsg.Builder> entry : map.entrySet()){
                MessageHelper.sendPacket(entry.getKey(), 0 , YanQuMessageUtils.buildMessage(GameProtocol.S_MONOPOLY_MARKET_NOTIFY_ADD_RANK_VALUE_FROM_CROSS,entry.getValue()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
